#ifndef _MODULE_REVENGE
#define _MODULE_REVENGE
#include "page.module.js"
#include "prefs.module.js"
#include "boss.module.js"
#include "console.module.js"
#include "task.lib.js"
#include "ajax.lib.js"
#include "cache.lib.js"
#include "utility.lib.js"
#include "usolib.lib.js"

Modules.push( "Revenge" );

var Revenge = {
	count: 0,
	running: false,
	status: {
		rank: 0,
		attack: false,
		punch: false,
		hitlist: false
	},
	user: [],
	user_id: '',
	last_action: false,
	ignore: [],
	last_reset: 0,
	log: function(action) {
		var log = [{
			name: 'Script',
			value: 'Revenge'
		},
		{
			name: 'Action',
			value: action
		}];
		Console.log(log);
		Console.status('revenge', action);
		return;
	},
	init: function(resume) {
		if (resume) {
			if (Revenge.running === false) Page.toggle('revenge');
			return;
		}
		Revenge.count = 0;
		Revenge.running = true;
		Revenge.status = {
			rank: 0,
			attack: false,
			punch: false,
			hitlist: false
		};
		if ( Settings.revenge_retaliate_temp === true )
			Settings.revenge_chainers = "";
		Revenge.user = [];
		Revenge.user_id = '';
		Revenge.last_action = false;
		Revenge.ignore = [];
		Revenge.last_reset = 0;
		Revenge.log('Initiating Revenge Automation');
		Ajax(Page.domain + '/mobwars/', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Revenge.init(true);
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Revenge.check_stats();
			return;
		});
		return;
	},
	ignore_check: function(user_id) {
		var array = Revenge.ignore;
		for (var i = array.length - 1; i >= 0; i--) {
			if (user_id == array[i]) {
				return true;
			}
		}
		return false;;
	},
	check_stats: function() {
		var updating = false;
		Revenge.log('Checking Boss\'s Stats');
		if (Boss.cash >= Settings.bank_limit && Settings.bank && Cache.banking === false) {
			Revenge.log('Banking ' + Utility.int_dollars(Boss.cash));
			Task.bank(Revenge.check_stats);
			updating = true;
		}
		if (Settings.revenge_stamina_stop >= Boss.stamina) {
			Revenge.log('Recharging Boss\'s Stamina');
			var timer = new Utility.timer({
				script: 'revenge',
				message: 'Waiting [[time]] for Stamina to recharge...',
				time: stamina_wait * (Settings.revenge_stamina_start - Boss.stamina),
				fn: function() {
					Ajax(Page.domain + '/mobwars/', function(xhr) {
						Page.captcha(xhr.responseText);
						if (Revenge.running === false) {
							return;
						};
						if (Page.pausing === true) {
							Page.pause_cb(function() {
								Revenge.check_stats();
								return;
							});
							return;
						};
						Boss.update(xhr.responseText);
						Revenge.check_stats();
						return;
					});
				}
			});
			timer.start();
			updating = true;
		} else if (Boss.health <= Settings.min_health && Settings.heal && Cache.healing === false) {
			Revenge.log('Healing your Boss');
			Task.heal(Revenge.check_stats);
			updating = true;
		}
		if (updating) return;
		Revenge.load_action();
		return;
	},
	load_action: function() {
		if ( Settings.revenge_retaliate === true )
			Ajax( Page.domain + "/mobwars/", function( xhr ) {
				Task.chain_check( xhr.responseText, function( user_id ) {
					var array = Utility.textarea_array( Settings.revenge_chainers );
					for ( var i = 0, user; user = array[ i++ ]; ) {
						if ( parseInt( user ) === parseInt( user_id ) )
							return;
					}
					array.push( user_id );
					Settings.revenge_chainers = array.join( "\n" );
					Revenge.log( 'Added a chainer: ' + user_id );
				});
			});

		Revenge.user = Utility.textarea_array(Settings.revenge_targets).concat( Utility.textarea_array( Settings.revenge_chainers ) );
		if (!Revenge.user.length > 0 && !Settings.revenge_retaliate) {
			var timer = new Utility.timer({
				time: 5,
				message: 'There are no targets set in Preferences! [[time]]',
				script: 'revenge',
				fn: function() {
					Page.toggle('revenge');
					return;
				}
			});
			timer.start();
			return;
		}
		else if ( !Revenge.user.length > 0 ) {
			Revenge.status.attack = false;
			Revenge.status.hitlist = false;
			Revenge.status.punch = false;
			Revenge.wait();
			return;
		}
		if (Revenge.user_id != Revenge.user[Revenge.status.rank]) {
			Revenge.status.attack = false;
			Revenge.status.hitlist = false;
			Revenge.status.punch = false;
		}
		if (Revenge.status.rank >= Revenge.user.length) {
			Revenge.status.rank = 0;
		}
		var ignore = true;
		var loop_reset = false;
		while (ignore) {
			if (Revenge.user_id = Revenge.user[Revenge.status.rank]) {
				ignore = Revenge.ignore_check(Revenge.user_id);
				if (ignore) Revenge.status.rank++;
			} else if (loop_reset === false) {
				Revenge.status.rank = 0;
				loop_reset = true;
			} else {
				Revenge.log('All targets have been ignored. Force reseting ignore list.');
				Revenge.ignore = [];
				Revenge.counter();
				Revenge.last_reset = Revenge.count;
				Revenge.wait();
				return;
			}
		}
		var action = false;
		Revenge.last_action = false;
		if (Settings.revenge_attack && Revenge.status.attack === false) {
			Revenge.status.attack = true;
			var timer = new Utility.timer({
				time: Page.wait(),
				message: 'Attacking Mob ' + Revenge.user_id + ' in [[time]]...',
				script: 'revenge',
				fn: function() {
					if (Revenge.running === false) {
						return;
					};
					if (Page.pausing === true) {
						Page.pause_cb(function() {
							Revenge.check_stats();
							return;
						});
						return;
					};
					Revenge.attack();
					return;
				}
			});
			timer.start();
			action = true;
		}
		if (Settings.revenge_punch && Revenge.status.punch === false) {
			if (action) return;
			Revenge.status.punch = true;
			var timer = new Utility.timer({
				time: Page.wait(),
				message: 'Punching Mob ' + Revenge.user_id + ' in [[time]]...',
				script: 'revenge',
				fn: function() {
					if (Revenge.running === false) {
						return;
					};
					if (Page.pausing === true) {
						Page.pause_cb(function() {
							Revenge.check_stats();
						});
						return;
					};
					Revenge.punch();
				}
			});
			timer.start();
			action = true;
		}
		if (Settings.revenge_hitlist && Revenge.status.hitlist === false) {
			if (action) return;
			Revenge.status.hitlist = true;
			var timer = new Utility.timer({
				time: Page.wait(),
				message: 'Hitlisting Mob ' + Revenge.user_id + ' in [[time]]...',
				script: 'revenge',
				fn: function() {
					if (Revenge.running === false) {
						return;
					};
					if (Page.pausing === true) {
						Page.pause_cb(function() {
							Revenge.check_stats();
							return;
						});
						return;
					};
					Revenge.hitlist();
					return;
				}
			});
			timer.start();
			action = true;
		}
		if (action) {
			Revenge.last_action = true;
			return;
		}
		Revenge.status.attack = false;
		Revenge.status.hitlist = false;
		Revenge.status.punch = false;
		Revenge.status.rank++;
		Revenge.wait();
		return;
	},
	punch: function() {
		Revenge.log('Punching Mob ' + Revenge.user_id);
		Ajax(Page.domain + '/mobwars/fight/do.php?action=punch&target_id=' + Revenge.user_id + '&force=1&from=profile', function(xhr) {
			Page.captcha(xhr.responseText);
			if (Revenge.running === false)
				return;
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Revenge.check_stats();
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Revenge.announce(Task.announce(xhr.responseText));
			Revenge.counter();
			if (!Revenge.last_action) Revenge.check_stats();
			else Revenge.load_action();
		});
	},
	hitlist: function() {
		Revenge.log('Hitlisting Mob ' + Revenge.user_id);
		Ajax(Page.domain + '/mobwars/hitlist/add.php?from=profile&target_id=' + Revenge.user_id, function(xhr) {
			if (bounty = xhr.responseText.match(new RegExp('name="bounty" value="(.*)"'))) {
				bounty = bounty[1];
				Ajax(Page.domain + '/mobwars/hitlist/do.php?action=add&success=profile&bounty=' + bounty + '&target_id=' + Revenge.user_id + '&next=add.php%3Ftarget_id%3D' + Revenge.user_id, function(xhr) {
					Page.captcha(xhr.responseText);
					if (Revenge.running === false) return;
					if (Page.pausing === true) {
						Page.pause_cb(function() {
							Revenge.check_stats();
							return;
						});
						return;
					};
					Boss.update(xhr.responseText);
					Revenge.announce(Task.announce(xhr.responseText));
					Revenge.counter();
					if (!Revenge.last_action) Revenge.check_stats();
					else Revenge.load_action();
					return;
				},
				'POST');
				return;
			}
			Revenge.counter();
			if (!Revenge.last_action) Revenge.check_stats();
			else Revenge.load_action();
			return;
		});
		return;
	},
	attack: function() {
		if (Revenge.running === false) return;
		Revenge.log('Attacking Mob ' + Revenge.user_id);
		Ajax(Page.domain + '/mobwars/fight/do.php?action=attack&force=1&target_id=' + Revenge.user_id + '&force=1&from=/profile/?user_id=' + Revenge.user_id, function(xhr) {
			Page.captcha(xhr.responseText);
			if (Revenge.running === false) return;
			if (Page.pausing === true) {
				Page.pause_cb(function() {
					Revenge.check_stats();
					return;
				});
				return;
			};
			Boss.update(xhr.responseText);
			Revenge.announce(Task.announce(xhr.responseText));
			Revenge.counter();
			if (!Revenge.last_action)
				Revenge.check_stats();
			else
				Revenge.load_action();
		});
	},
	announce: function(ann) {
		if (ann) {
			if (ann.won)
				Revenge.log('Fight Won against ' + Revenge.user_id + '! You dealt ' + ann.damaged + ' damage, recieved ' + ann.damager + ', gained ' + ann.cash_str + ' and ' + ann.exp + ' exp');
			else if (ann.lost)
				Revenge.log('Fight Lost against ' + Revenge.user_id + '! You dealt ' + ann.damaged + ' damage, recieved ' + ann.damager);
			else if (ann) {
				Revenge.log('Fight Attack failed against ' + Revenge.user_id + '! "' + ann.message + '"');
				Revenge.ignore.push(Revenge.user_id);
			}
		}
	},
	counter: function() {
		Revenge.count++;
		if (Revenge.count >= Settings.script_repeat && Settings.script_repeat != -1) {
			Revenge.log('Automation Finished!');
			Page.toggle('revenge');
		}
		if (Revenge.count - Revenge.last_reset >= Settings.revenge_ignore_reset && Settings.revenge_ignore_reset != 0) {
			Revenge.ignore = [];
			Revenge.last_reset = Revenge.count;
		}
		return;
	},
	wait: function() {
		if (Revenge.running === false) return;
		if (Settings.revenge_wait > 0) {
			var timer = new Utility.timer({
				time: Settings.revenge_wait,
				message: 'Waiting to attack next target in [[time]]...',
				script: 'revenge',
				fn: function() {
					if (Revenge.running === false) return;
					if (Page.pausing === true) {
						Page.pause_cb(function() {
							Revenge.check_stats();
							return;
						});
						return;
					};
					Revenge.check_stats();
					return;
				}
			});
			timer.start();
			return;
		}
		Revenge.check_stats();
		return;
	}
};
#endif
